﻿using Common.Communication;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Common.Tool.DataTools;
using Common.Tool.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Device.Models
{
    public class CeLiangYi : Device
    {
        /// <summary>
        /// 测试模式
        /// </summary>
        public Enums.CeLiangYiEnum.TestMode mode { get; set; }
        public string 测量类型 { get; set; }
        public int 通道 { get; set; }
        public double 测量电平 { get; set; }
        /// <summary>
        /// 生成测试指令
        /// </summary>
        /// <param name="deviceName">仪器名称</param>
        /// <returns></returns>
        public override Order GetOrder(string deviceName)
        {  
            //测试指令
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            //测试模式
            switch (mode)
            {
                case Enums.CeLiangYiEnum.TestMode.测量电压:
                    order.range = GetRange("V");
                    order.deviceModel = JsonConvert.SerializeObject(new { 测量类型, 通道, 上限值, 下限值 });
                    break;
                case Enums.CeLiangYiEnum.TestMode.测量电流:
                    order.range = GetRange("A");
                    order.deviceModel = JsonConvert.SerializeObject(new { 测量类型, 通道, 上限值, 下限值 });
                    break;
                case Enums.CeLiangYiEnum.TestMode.测量电阻:
                    order.range = GetRange("Ω");
                    order.deviceModel = JsonConvert.SerializeObject(new { 测量类型, 通道, 上限值, 下限值 });
                    break;
                case Enums.CeLiangYiEnum.TestMode.短路测试:
                    order.range = GetRange("Ω");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 上限值, 下限值 });
                    break;
                case Enums.CeLiangYiEnum.TestMode.二极管测试:
                    order.range = GetRange("V");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 上限值, 下限值 });
                    break;
                case Enums.CeLiangYiEnum.TestMode.测量电容:
                    order.range = GetRange("F");
                    order.deviceModel = JsonConvert.SerializeObject(new { 测量类型, 测量电平, 通道, 上限值, 下限值 });
                    break;
                case Enums.CeLiangYiEnum.TestMode.测量电感:
                    order.range = GetRange("H");
                    order.deviceModel = JsonConvert.SerializeObject(new { 测量类型, 测量电平, 通道, 上限值, 下限值 });
                    break;
                default:
                    break;
            }
            return order;
        }
        /// <summary>
        /// 测试并返回测试结果
        /// </summary>
        /// <param name="dev">仪器实体</param>
        /// <param name="testOrder">测试指令</param>
        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {
            //仪器实体
            var result = new DeviceResult();
            //测试结果实体
            Result<double> r1 = new Result<double>();
            //测试模式
            switch (mode)
            {
                case CeLiangYiEnum.TestMode.测量电压:
                    //万用表测电压
                    if (dev is IWanYongBiao)
                    {
                        r1 = (dev as IWanYongBiao).GetVoltage(通道, (CeLiangYiEnum.VoltageMode)Enum.Parse(typeof(CeLiangYiEnum.VoltageMode), 测量类型));
                    }
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "V";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                case CeLiangYiEnum.TestMode.测量电流:
                    //万用表测电流
                    if (dev is IWanYongBiao)
                    {
                        r1 = (dev as IWanYongBiao).GetCurrent(通道, (CeLiangYiEnum.CurrentMode)Enum.Parse(typeof(CeLiangYiEnum.CurrentMode), 测量类型));
                    }
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "A";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                case CeLiangYiEnum.TestMode.测量电阻:
                    //万用表测电阻
                    if (dev is IWanYongBiao)
                    {
                        r1 = (dev as IWanYongBiao).GetResistance(通道, (CeLiangYiEnum.ResistanceMode)Enum.Parse(typeof(CeLiangYiEnum.ResistanceMode), 测量类型));
                    }
                    //电阻仪测电阻
                    if (dev is IDianZuYi)
                    {
                        r1 = (dev as IDianZuYi).GetResistance(通道, (CeLiangYiEnum.ResistanceMode)Enum.Parse(typeof(CeLiangYiEnum.ResistanceMode), 测量类型));
                    }
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "Ω";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                case CeLiangYiEnum.TestMode.短路测试:
                    //万用表短路测试
                    if (dev is IWanYongBiao)
                    {
                        r1 = (dev as IWanYongBiao).GetContinuity(通道);
                    }
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "Ω";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                case CeLiangYiEnum.TestMode.二极管测试:
                    //万用表二极管测试
                    if (dev is IWanYongBiao)
                    {
                        r1 = (dev as IWanYongBiao).GetDiode(通道);
                    }
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "V";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                case CeLiangYiEnum.TestMode.测量电容:
                    //数字电桥测电容
                    if (dev is IShuZiDianQiao)
                    {
                        r1 = (dev as IShuZiDianQiao).GetDianRong(测量电平, 通道, (CeLiangYiEnum.DianRongMode)Enum.Parse(typeof(CeLiangYiEnum.DianRongMode), 测量类型));
                    }
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "F";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                case CeLiangYiEnum.TestMode.测量电感:
                    //数字电桥测电感
                    if (dev is IShuZiDianQiao)
                    {
                        r1 = (dev as IShuZiDianQiao).GetDianGan(测量电平, 通道, (CeLiangYiEnum.DianGanMode)Enum.Parse(typeof(CeLiangYiEnum.DianGanMode), 测量类型));
                    }
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "H";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                default:
                    break;
            }
            //上下限判断
            if (下限值 != null && result.success)
            {
                result.success = r1.Value >= 下限值;
            }
            if (上限值 != null && result.success)
            {
                result.success = r1.Value <= 上限值;
            }
            return result;
        }
    }
}
